<template>
  <div class="reception-application">
    <div class="header">
      <router-link to="/main/xingzheng/jiedai/process" class="back-btn">
        <i class="el-icon-arrow-left"></i>
        <span>返回</span>
      </router-link>
      <h2>接待申请</h2>
    </div>

    <div class="content">
      <el-form :model="formData" ref="form" label-width="120px" class="application-form">
        <el-form-item label="申请编号">
          <el-input v-model="formData.applicationNo" disabled></el-input>
        </el-form-item>

        <el-form-item label="申请部门">
          <el-input v-model="formData.department"></el-input>
        </el-form-item>

        <el-form-item label="申请人">
          <el-input v-model="formData.applicant"></el-input>
        </el-form-item>

        <el-form-item label="接待对象">
          <el-input v-model="formData.guestName" placeholder="来访客人姓名"></el-input>
        </el-form-item>

        <el-form-item label="来访单位">
          <el-input v-model="formData.guestCompany"></el-input>
        </el-form-item>

        <el-form-item label="来访人数">
          <el-input-number v-model="formData.guestCount" :min="1"></el-input-number>
        </el-form-item>

        <el-form-item label="接待时间">
          <el-date-picker
            v-model="formData.receptionTime"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="接待事由">
          <el-input type="textarea" v-model="formData.purpose" rows="4"></el-input>
        </el-form-item>

        <el-form-item label="接待地点">
          <el-input v-model="formData.location"></el-input>
        </el-form-item>

        <el-form-item label="接待类型">
          <el-select v-model="formData.receptionType">
            <el-option label="商务接待" value="商务接待"></el-option>
            <el-option label="技术交流" value="技术交流"></el-option>
            <el-option label="项目洽谈" value="项目洽谈"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="预计费用">
          <el-input-number v-model="formData.estimatedCost" :min="0"></el-input-number>
          <span class="unit">元</span>
        </el-form-item>

        <el-form-item label="费用说明">
          <el-input type="textarea" v-model="formData.costDescription" rows="3"></el-input>
        </el-form-item>

        <el-form-item label="特殊要求">
          <el-input type="textarea" v-model="formData.specialRequirements" rows="3"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm" :loading="submitting">提交申请</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      submitting: false,
      formData: {
        applicationNo: this.generateApplicationNo(),
        department: '',
        applicant: '',
        guestName: '',
        guestCompany: '',
        guestCount: 1,
        receptionTime: [],
        purpose: '',
        location: '',
        receptionType: '',
        estimatedCost: 0,
        costDescription: '',
        specialRequirements: ''
      },
      rules: {
        department: [
          { required: true, message: '请输入申请部门', trigger: 'blur' }
        ],
        applicant: [
          { required: true, message: '请输入申请人', trigger: 'blur' }
        ],
        guestName: [
          { required: true, message: '请输入接待对象', trigger: 'blur' }
        ],
        guestCompany: [
          { required: true, message: '请输入来访单位', trigger: 'blur' }
        ],
        guestCount: [
          { required: true, message: '请输入来访人数', trigger: 'blur' },
          { type: 'number', min: 1, message: '来访人数必须大于0', trigger: 'blur' }
        ],
        receptionTime: [
          { required: true, message: '请选择接待时间', trigger: 'change' }
        ],
        purpose: [
          { required: true, message: '请输入接待事由', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请输入接待地点', trigger: 'blur' }
        ],
        receptionType: [
          { required: true, message: '请选择接待类型', trigger: 'change' }
        ],
        estimatedCost: [
          { required: true, message: '请输入预计费用', trigger: 'blur' },
          { type: 'number', min: 0, message: '预计费用不能为负数', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          try {
            this.submitting = true
            
            const [startTime, endTime] = this.formData.receptionTime
            const submitData = {
              ...this.formData,
              receptionStartTime: startTime,
              receptionEndTime: endTime
            }
            delete submitData.receptionTime
            
            const response = await this.$http.post('/api/reception/submit', submitData)
            
            this.$message.success('申请提交成功')
            this.$router.push('/main/xingzheng/jiedai/process')
          } catch (error) {
            const errorMsg = error.response?.data || '提交失败，请稍后重试'
            this.$message.error(errorMsg)
          } finally {
            this.submitting = false
          }
        } else {
          this.$message.warning('请填写完整的申请信息')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.form.resetFields()
      this.formData.applicationNo = this.generateApplicationNo()
      this.formData.receptionTime = []
      this.formData.guestCount = 1
      this.formData.estimatedCost = 0
    },
    generateApplicationNo() {
      const date = new Date()
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const random = String(Math.floor(Math.random() * 1000)).padStart(3, '0')
      return `JD${year}${month}${day}${random}`
    }
  }
}
</script>

<style lang="scss" scoped>
.reception-application {
  padding: 20px;

  .header {
    margin-bottom: 30px;
    position: relative;
    text-align: center;

    .back-btn {
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      text-decoration: none;
      color: #606266;
      display: flex;
      align-items: center;
    }

    h2 {
      margin: 0;
      font-size: 24px;
      color: #303133;
    }
  }

  .content {
    background: #fff;
    padding: 20px;
    border-radius: 8px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
  }

  .application-form {
    max-width: 800px;
    margin: 0 auto;
  }

  .unit {
    margin-left: 10px;
    color: #606266;
  }
}
</style> 